<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Token extends Base
{
    protected $table='token';


    /**
     * 验证token
     * @param $token
     */
    public static function check($token)
    {
        $tokens = self::where('token',$token)->get();

        if($tokens->isEmpty()){
            //token不存在
            return 400;
        }else{

            //判断token是否失效
            if(strtotime($tokens[0]['updated_at'])+30*3600*24<=time()){
                return 401;
            }else{
                //更新token时间
                $rew = self::where('token',$token)->update([
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ]);
                if($rew){
                    return $tokens[0]['uid'];
                }
            }

        }
    }
}
